function create_vm (
	[int] $scenario,  #scenario 0..#s-1
	[int] $computer #ordinateur 0..#c-1
) { 
	$VMServer = $CONFIG_SERVERS[$computer]
	$sujet = $sujets[$scenario]
	
	$checks = @("name";"cpu";"ram";"vfd;vhd_and_iso");
	
	foreach($vm in $sujet.vms) {
		$VMName = (($sujet.scenario_name)+"-"+($vm.name))
		
		(banner($VMName)) | write-host -foregroundcolor yellow
				
		#check the virtual machine parameters
		$ERROR = $FALSE
		foreach($check in $checks) {
			$ch_built = ""
			foreach($ch_unit in $check.split(";")) { $ch_built += '$vm.'+$ch_unit+' -OR ' }
			$ch_built = $ch_built.substring(0,$ch_built.Length-4);
			$exec = ('$ch = ('+$ch_built+');')
			invoke-expression $exec
			if(-NOT($ch)) {
				write-host ("  ->ERROR: the parameter "+$check+" is not set") -foregroundcolor red
				$ERROR=$TRUE
			}
		}
		
		if(-NOT $ERROR) {
			#check if the virtual machine already exists
			
			$vm_found = (get-vm -Server $VMServer -name $VMName)
			
			if($vm_found) {
				#if yes ask to delete it
				write-host (" WARNING: "+($vm_found.Count)+" virtual machine(s) have the same name. ") -foregroundcolor red
				do {
					write-host ("DELETE the virtual machines named '"+$VMName+"'? [y/n]") -foregroundcolor yellow
					if(Host-IsInactiveForSeconds 10) {
						$snap = "y"
					} else {
						$snap = read-host -prompt "?"
					}
				} while( $snap.CompareTo("y") -AND $snap.CompareTo("n"))

				if(!$snap.CompareTo("y")) {
				do {
					write-host "Are you SURE? [y/n]" -foregroundcolor yellow
					if(Host-IsInactiveForSeconds 10){
						$sure = "y"
					} else {
						$sure = read-host -prompt "?"
					}
				} while($sure.CompareTo("y") -AND $sure.CompareTo("n"))
				if(!$sure.CompareTo("n")) { $snap = "n" }
				}
				if($snap -eq "y") {
					foreach($vm_to_del in $vm_found) {
						write-host ("Deleting "+$vm_to_del.ElementName+"...") -foregroundcolor yellow 
						$vm_to_del | remove-vm -force -wait
					}
				}
			}
			
			Remove-variable "vm_found"
			$vm_found = (get-vm -Server $VMServer -name $VMName)
			#write-host $vm_found
			if(-NOT $vm_found) {
				# finally create it
				$path_additions = ($sujet.scenario_name+"\"+$vm.name+"\")
				#create it
				$vm_wmi = new-vm -name $VMName -path ($CONFIG_CLIENT_VM_STORAGE_DIR_LOCAL_VISION+$path_additions) -server $VMServer
				
				#$vm_wmi = get-vm -name $VMName -Server $VMServer
				if($vm_wmi) {
					write-host (" - virtual machine '"+$VMName+"': creation started")
				
					#boot
					if($vm.boot) {
						if(invoke-expression('$vm_wmi | set-vm -bootorder '+$vm.boot)) {
							write-host (" - Boot: OK ("+$vm.boot+")") -foregroundcolor yellow
						} else { write-host (" - Boot: ERROR while setting the order '"+$vm.boot+"'. Please remember this should be a comma separated string like CD,IDE,net,Floppy") -foregroundcolor red}
					} else { write-host (" - Boot: No order specified. Will be ISO, VHD, PXE, VFD") -foregroundcolor magenta }
				
					#cpu
					if(set-vmcpucount $vm_wmi -cpucount $vm.cpu) { write-host (" - CPU: OK ("+$vm.cpu+")") } else { write-host (" - CPU: ERROR ("+$vm.cpu+")") -foregroundcolor red }
					
					#ram
					if(set-vmmemory $vm_wmi -memory $vm.ram) { write-host (" - RAM: OK ("+$vm.ram+")") } else { write-host (" - RAM: ERROR ("+$vm.ram+")") -foregroundcolor red }
					
					#vhd or iso
					if($vm.vhd_and_iso) {
						$count = 0
						foreach($disk in $vm.vhd_and_iso) {
							$arr = $disk.split("\");
							$vhd_short_name = $arr[$arr.length-1]
							$vhd_path = ($CONFIG_CLIENT_VM_STORAGE_DIR_LOCAL_VISION+$path_additions+$vhd_short_name)
							write-host ("Trying to add the disk "+$vhd_path+" on server $VMServer") -foregroundcolor yellow
							
							$ext = $arr[$arr.length-1].substring($arr[$arr.length-1].length-3,3)
							$command = 'add-vmdisk $vm_wmi -VHDPath $vhd_path -ControllerID '+([Math]::Floor($count / 2))+' -LUN '+([Math]::Floor($count%2))
							
							if($ext -eq "iso") { $command += ' -OpticalDrive'}
							#write-host $command
							
							if(invoke-Expression $command) {
								write-host " - $ext: OK "
							} else {
								write-host (" - $ext: ERROR ("+$vhd_path+")") -foregroundcolor red 
							}
							$count++
						}
					}  else { write-host ("WARNING: no VIRTUAL DISK created, because no VHD/ISO was specified for the vm "+$vm.name+" in "+($scenario+1)+".ps1. Check the VM_AND_ISO attribute. ") -foregroundcolor magenta }

				
					# vfd
					if($vm.vfd) {
						$arr = $vm.vfd.split("\");
						$vhd_short_name = $arr[$arr.length-1]
						$vhd_path = ($CONFIG_CLIENT_VM_STORAGE_DIR_LOCAL_VISION+$path_additions+$vhd_short_name)
						write-host ("Trying to mount the floppy disk "+$vhd_path+" on server $VMServer") -foregroundcolor yellow
						if(add-vmfloppydisk $vm_wmi -Path $vhd_path) {
							write-host " - VFD: OK "
						} else {
							write-host (" - VFD: ERROR ("+$vhd_path+")") -foregroundcolor red 
						}
					} else {write-host ("WARNING: no VIRTUAL FLOOPY DISK created, because no VFD was specified for the vm "+$vm.name+" in "+($scenario+1)+".ps1") -foregroundcolor magenta}
					
					
					#nic
					foreach($nic in $vm.nic) {
						#check if the network is external or not
						$is_external = $FALSE
						foreach($netw in $sujet.virtual_networks) {
							if(($netw.name -eq $nic.network) -AND ($netw.type -eq "external")) {
								$is_external = $TRUE
							}
						}
						if($is_external) { $switchname = $DEFAULT_EXTERNAL_SWITCHNAME }
						else { $switchname = $sujet.scenario_name+"_"+$nic.network }
						
						$command = '$nic2 = add-vmnic $vm_wmi -virtualSwitch $switchname -force'
						if($nic.type -eq "legacy") { $command += " -legacy" }
						if($nic.guid) { $command += (' -guid "'+$nic.guid+'"'); $guid = $nic.guid } else { $guid = "New guid" }
						invoke-expression $command
						if($nic2.Connection[0].Trim().Length -ne 0) { write-host " - NIC: OK ($switchname,$guid)" } else { write-host " - NIC: error($switchname,$guid). Most probably the network name is not valid: '$switchname' " -foregroundcolor red}
					}
				} else {
					write-host "ERROR while creating the virtual machine" -foregroundcolor red
				}
			} else {
				write-host "ERROR: the virtual machine already exists. It will not be created again." -foregroundcolor red
			}
			Remove-variable "vm_found"
			
		}		
	}
}

$actions.Insert($actions.Count,"create_vm");
set-variable LANG_ACTION__CREATE_VM "CREER les VM" -scope global
